package com.jinshan.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinshan.edu.entity.EduSubject;
import com.jinshan.edu.entity.SubjectLevelHead;
import com.jinshan.edu.listener.SubjectAnalysisListener;
import com.jinshan.edu.mapper.EduSubjectMapper;
import com.jinshan.edu.service.EduSubjectService;
import com.jinshan.exception.EduException;
import com.jinshan.response.SubjectVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author Jinshan
 * @since 2020-06-24
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    // @Autowired
    // private EduSubjectMapper eduSubjectMapper;
    @Autowired
    private SubjectAnalysisListener subjectAnalysisListener;

    @Override
    public void upload(MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        EasyExcel.read(inputStream, SubjectLevelHead.class, subjectAnalysisListener).doReadAll();
    }

    @Override
    public EduSubject querySubjectByTitleAndParentId(String title, String parentId) {
        QueryWrapper<EduSubject> eduSubjectQueryWrapper = new QueryWrapper<>();
        eduSubjectQueryWrapper.eq("title", title);
        eduSubjectQueryWrapper.eq("parent_id", parentId);
        // return eduSubjectMapper.selectOne(eduSubjectQueryWrapper);
        return baseMapper.selectOne(eduSubjectQueryWrapper);
    }

    @Override
    public void addEduSubject(EduSubject eduSubject) {
        // eduSubjectMapper.insert(eduSubject);
        baseMapper.insert(eduSubject);
    }

    @Override
    public List<SubjectVO> getAllSubject() {
        // List<EduSubject> eduSubjects = eduSubjectMapper.selectList(null);
        List<EduSubject> eduSubjects = baseMapper.selectList(null);
        ArrayList<SubjectVO> subjectVOS = new ArrayList<SubjectVO>();
        // 通过parentId=0找出所有的一级分类
        for (EduSubject eduSubject : eduSubjects) {
            if ("0".equals(eduSubject.getParentId())) {
                SubjectVO subjectVO = new SubjectVO();
                BeanUtils.copyProperties(eduSubject, subjectVO);
                subjectVOS.add(subjectVO);
            }
        }

        // 将一级分类SubjectVO建立以自身id为键, 对象本身位值的Map集合
        HashMap<String, SubjectVO> subjectVOHashMap = new HashMap<>();
        for (SubjectVO subjectVO : subjectVOS) {
            subjectVOHashMap.put(subjectVO.getId(), subjectVO);
        }

        // 遍历所有的分类, 找出所有的二级分类, 并根据它的parentId关联对应的一级分类
        for (EduSubject eduSubject : eduSubjects) {
            if (!"0".equals(eduSubject.getParentId())) {
                SubjectVO parentSubjectVO = subjectVOHashMap.get(eduSubject.getParentId());
                SubjectVO childSubjectVO = new SubjectVO();
                BeanUtils.copyProperties(eduSubject, childSubjectVO);
                parentSubjectVO.getChildren().add(childSubjectVO);
            }
        }
        return subjectVOS;
    }

    @Override
    public boolean addFirstLevelSubject(EduSubject eduSubject) {
        EduSubject existEduSubject = querySubjectByTitleAndParentId(eduSubject.getTitle(), eduSubject.getParentId());
        if (existEduSubject == null) {
            int rows = baseMapper.insert(eduSubject);
            return rows > 0;
        }
        throw new EduException(20001, "添加的一级分类已存在!");
    }

    @Override
    public boolean addSubject(EduSubject eduSubject) {
        if ("".equals(eduSubject.getParentId())) {
            eduSubject.setParentId("0");
        }
        EduSubject existEduSubject = querySubjectByTitleAndParentId(eduSubject.getTitle(), eduSubject.getParentId());
        if (existEduSubject == null) {
            int rows = baseMapper.insert(eduSubject);
            return rows > 0;
        }
        throw new EduException(20001, "添加的课程分类已存在!");
    }

    @Override
    public boolean deleteSubjectById(String id) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        // 通过传入的id作为parent_id去查该id下面是否有其他的课程分类
        Integer count = baseMapper.selectCount(wrapper);
        // 如果查询结果大于0, 说明有子节点, 不能直接删除
        if (count > 0) {
            return false;
        }
        int rows = baseMapper.deleteById(id);
        return rows > 0;
    }

    @Override
    public void deleteSubjectByCourseId(String courseId) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id" , courseId);
        baseMapper.delete(wrapper);
    }

}
